package top.linyuxb.lycloud.rpc.starter.common;

import java.io.*;
import java.util.Base64;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2023/10/31 17:37
 * @Desc:
 */
public class FileUtils {
    /**
     * 文件转二进制
     *
     * @param filePath
     * @return
     */
    public static byte[] file2Bytes(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) return new byte[0];
        try (InputStream inputStream = new FileInputStream(file);
             BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ) {
            byte[] buf = new byte[1024];
            int len;
            if ((len = bufferedInputStream.read(buf)) >= 0) {
                outputStream.write(buf, 0, len);
            }
            return outputStream.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将二进制流转换为文件
     *
     * @param bytes
     * @return
     */
    public static String bytes2File(byte[] bytes, String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (FileOutputStream outputStream = new FileOutputStream(fileName);) {
            outputStream.write(bytes);
            return fileName;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将文件转base64
     *
     * @param filePath 文件路径
     */
    public static String fileToBase64(String filePath) {
        return fileToBase64(new File(filePath));
    }

    /**
     * 将文件转base64
     *
     * @param file 文件
     */
    public static String fileToBase64(File file) {
        try (InputStream fileInputStream = new FileInputStream(file);
             BufferedInputStream buf = new BufferedInputStream(fileInputStream);
             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();) {
            int len;
            byte[] bytes = new byte[1024];
            while ((len = buf.read(bytes)) != -1) {
                byteArrayOutputStream.write(bytes, 0, len);
            }
            return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将base64字符串转文件
     *
     * @param base64Str base64字符串
     */
    public static void base64ToFile(String base64Str, String fileName) {
        //解码
        byte[] bytes = Base64.getDecoder().decode(base64Str);
        try (FileOutputStream outputStream = new FileOutputStream(fileName);) {
            outputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static File getBase64ToFile(String base64Str, String fileName) {
        try {
            String[] split = fileName.split("\\.");
            File file = File.createTempFile(split[0], "." + split[1]);
            //解码
            byte[] bytes = Base64.getDecoder().decode(base64Str);
            try (FileOutputStream outputStream = new FileOutputStream(file);) {
                outputStream.write(bytes);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将内容写入文件
     *
     * @param base64Str
     * @param filePath
     */
    public static void strToFile(String base64Str, String filePath) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fileWriter);
            bw.write(base64Str);
            bw.close();
            System.out.println("finish");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取文件内容
     *
     * @param path
     * @return
     */
    public static String getFileContent(String path) {
        String fileContent = "";
        try {
            File f = new File(path);
            if (f.isFile() && f.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(f), "utf-8");
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
                    fileContent += line;
                }
                read.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileContent;
    }

//    public static void main(String[] args) {
////        String path = "/Users/hfb/meili/fbhou/104/Documents/workspace/yearly-review.zip";
//        String path = "/Users/hfb/hfb/workspace/归档.zip";
//        String path1 = "/Users/hfb/hfb/归档.txt";
//        // zip -> base64
//        String s = fileToBase64(path);
//        // base64 -> txt
//        strToFile(s,path1);
//        // txt -> string
//        String fileContent = getFileContent(path1);
//        // string -> zip
//        base64ToFile(fileContent,path);
//    }
}
